var utils = require("./utils");

utils.exitIfRepeat();

//监听start事件
events.on("start", function(){
    main(arguments);
});
setInterval(()=>{}, 1000);

let video=function(times,nextFunction){
    this.times = times;
    this.nextFunction = nextFunction;
    this.start = function(){
        var num = 1;
        while (num++ <= times) {
            nextFunction.call();
            sleepTime = random(10, 15);
            toast("剩余观看数："+(times-num+1));
            sleep(sleepTime * 1000);
        }
        toast("结束观看");
    }
}
// 通用观看小视频方法
let common = {
    watch: function (runTimes) {
        toast("打开APP"+this.appName);
        app.launch(this.packageName);
        waitForPackage(this.packageName);
        toast("进入到APP"+this.appName);
        if(this.backThread){
            this.backThread();           
        }
        //签到
        if(this.checkin){
            this.checkin();
        }
        new video(runTimes,()=>this.nextVideo()).start();
        home();
    }
}
var appMaps = {
    //刷宝短视频
    shuabao : {
        appName:"刷宝短视频",
        packageName:"com.jm.video",
        nextVideo:function(){
            id("tv_tab_title").className("android.widget.TextView").text("首页").findOne().parent().parent().click();
        },
        checkin:function(){
            let 任务页签 = id("tv_tab_title").className("android.widget.TextView").text("任务");
            // 点击任务标签
            任务页签.findOne().parent().parent().click();
            //等待任务页面
            textContains("签到获得").waitFor();
            if(textContains("今日可签到获得").exists()){
                sleep(1000);
                text("立即签到").className("android.widget.Button").findOne().click();
                sleep(1000)
                text("看视频签到").className("android.view.View").clickable().findOne().click();
                let countdown = id("tv_countdown").textMatches(/\d+S/);
                广告时长 = countdown.findOne(5000);
                var closeBtnId;
               if(广告时长 != null){
                    closeBtnId = "iv_close";
               }else{
                    closeBtnId = "tt_video_ad_close_layout";
               }
               id(closeBtnId).findOne().click();
            }else{
                toastLog("已经签到");
            }
        },
        backThread:function(){
            threads.start(function(){
                while(true){
                    var yaoqingClose = id("imgClose");
                    if(yaoqingClose.exists()){
                        var w = yaoqingClose.findOne(1000);
                        if(w != null){
                            w.click();
                        }
                    }
                    sleep(1000);
                }
            });
            threads.start(function(){
                let 限时奖励 = id("com.jm.video:id/home_page_time_reward");
                // toastLog(限时奖励.findOne().childCount())
                if(限时奖励.exists() && 限时奖励.findOne().childCount() == 2){
                    限时奖励.findOne().child(0).click();
                    id("com.jm.video:id/tv_go").findOne().click();
                }
            });
        }

    },

    //抖音极速
    douyin : {
    appName:"抖音极速",
    packageName:"com.ss.android.ugc.aweme.lite",
    nextVideo:function(){
        id("com.ss.android.ugc.aweme.lite:id/cwp").findOne().child(0).click();
    }
    }
};




function main(config){
    var times = 0;
    while(true){
        var apps = config;
        var size = apps.length;
        var appId = apps[times++%size];
        var curApp = appMaps[appId];
        if(curApp){
            common.watch.call(curApp,random(50,100));
            sleep(2000);
        }else{
            toastLog(appId + "不存在");
        }
    }
}